package com.example.library_base.base.adapter

import android.util.SparseIntArray
import androidx.annotation.LayoutRes

abstract class ItemViewType<T> {

    companion object{
        private const val DEFAULT_VIEW_TYPE = -0xff
    }

    private val layouts: SparseIntArray = SparseIntArray()

    abstract fun getItemType(t: T): Int

    fun getDefItemViewType(data: List<T>, position: Int): Int{
        val item = data[position]
        return getItemType(item)
    }

    fun getLayoutId(viewType: Int): Int{
        return layouts.get(viewType)
    }

    fun registerItemType(type: Int, @LayoutRes layoutId: Int): ItemViewType<T>{
        addItemType(type, layoutId)
        return this
    }

    private fun addItemType(type: Int, @LayoutRes layoutId: Int){
        layouts.put(type, layoutId)
    }
}